<?php
class GeneralController extends Zend_Controller_Action 
{
	private $m_AppUI; // bien luu thong tin dang nhap

	public function init()
	{
		$this->objGeneral = Application_Model_General::getInstance();
	}

	public function autoresponsesAction()
	{
		$params = array(
			'keywords' => $this->_request->getParam('keywords'),
			'section' => $this->_request->getParam('section'),
			'page' => $this->_request->getParam('page', 1),
			'limit' => $this->_request->getParam('limit', 30)
		);
		$this->view->params = $params;
		$this->view->objGeneral = $this->objGeneral;
		$this->view->arrItems = $this->objGeneral->searchAutoResponse($params, $total);
		$params['link'] = BASE_URL.'general/autoresponses?'.http_build_query(array_slice($params, 0, -2));
		$params['total'] = $total;
		$this->view->PAGE_LINKS = Common::generatePaginator($params);
	}

	public function autoresponseAction()
	{
		$response_id = $this->_request->getParam('id');
		if($this->_request->getPost('action')=='save')
		{
			$arrData = array(
				'id' => $this->_request->getPost('id'),
				'key' => $this->_request->getPost('key'),
				'object' => $this->_request->getPost('object'),
				'title_en' => $this->_request->getPost('title_en'),
				'title_vi' => $this->_request->getPost('title_vi'),
				'content_en' => $this->_request->getPost('content_en'),
				'content_vi' => $this->_request->getPost('content_vi')
			);
			$id = $this->objGeneral->saveAutoResponse($arrData);
			$this->_redirect(BASE_URL.'general/autoresponse?id='.$id.'&msg=data_saved');
		}
		$this->view->objGeneral = $this->objGeneral;
		$this->view->arrItem = $this->objGeneral->getAutoResponse($response_id);
	}

	public function languagesAction()
	{
		$strPath = APP_PATH.'/locales/';	
		if($this->_request->getPost('action')=='save')
		{
			$strFile = $this->_request->getPost('file');
			$strFilePath = $strPath.$strFile;
			if(file_exists($strFilePath))
			{
				$keys = $this->_request->getPost('keys');
				$arrEN = $this->_request->getPost('lang_en');
				$arrVN = $this->_request->getPost('lang_vn');

				$strContent = '<?xml version="1.0" encoding="UTF-8"?>
	<tmx version="2.0" xmlns="http://www.lisa.org/tmx20">
	<header adminlang="en" creationtool="locale4j" creationtoolversion="1.1" o-tmf="unknown" segtype="block" srclang="*all*"/>
<body>';
				foreach($keys as $id=>$key)
				{
					$key = trim($key);
					if(empty($key)) continue;
					$strTmp = '<tu tuid="'.htmlspecialchars($key).'">
	<tuv xml:lang="en"><seg>'.htmlspecialchars($arrEN[$id]).'</seg></tuv>
	<tuv xml:lang="vi"><seg>'.htmlspecialchars($arrVN[$id]).'</seg></tuv>
</tu>';
					$strContent.= $strTmp;
				}
				$strContent.= '</body>
</tmx>';
				if($fp = fopen($strFilePath, 'w'))
				{
					fwrite($fp, $strContent);
					fclose($fp);
					echo _t('language_saved');
				}
				else
				{
					echo _t('cannot_save_file').': '.$strFilePath;
				}
				// reset language cache
				$strCache = $strFilePath;
				$strCache = str_replace($strPath, '', $strCache);
				$strCache = strtr($strCache, array(
					'/' => '_',
					'.tmx' => '',
					'common' => 'index'
				));
				foreach(array('en', 'vi') as $lang)
				{
					$strCacheName = 'Lang_'.$lang.'_'.$strCache;
					Zend_Translate::clearCache($strCacheName);
				}
				exit();
			}
		}
		// load language
		elseif($strFile = $this->_request->getPost('file'))	
		{
			$this->_helper->layout->disableLayout();
			$translate = new Zend_Translate ( 'tmx', $strPath.$strFile, 'vi', array ('scan' => Zend_Translate::LOCALE_FILENAME ) );
			$this->view->strFile = $strPath.$strFile;
			$this->view->arrVI = $translate ->getMessages('vi');
			$this->view->arrEN = $translate ->getMessages('en');
			return;
		}

		// list file language
		$arrFiles = array();
		$arrDirs = array('admin', 'jobseekers', 'employers');
		foreach($arrDirs as $dir)
		{
			$strDir = $strPath.$dir;
			if(!$hndl = opendir($strDir)) return;
			while($file = readdir($hndl))
			{
				if(strpos($file, '.tmx') === FALSE) continue;
				$arrFiles[$dir][] = array($file, floor(filesize($strDir.'/'.$file)/1024).'KB');
			}
			closedir($hndl);
		}
		$this->view->arrFiles = $arrFiles;
	}

	public function locationsAction()
	{
		$strAction = $this->_request->getPost('action');
		switch($strAction)
		{
			case 'save':
				$arrData = array(
					'location_id' => $this->_request->getPost('id'),
					'location_name_en' => $this->_request->getPost('name_en'),
					'location_name_vi' => $this->_request->getPost('name_vi'),
					'location_priority' => $this->_request->getPost('priority'),
					'location_status' => $this->_request->getPost('publish')
				);
				$this->objGeneral->saveLocation($arrData);
				$this->_redirect(BASE_URL.'general/locations?msg=data_saved');
				break;
			case 'delete':
				$ids = $this->_request->getPost('ids');
				$this->objGeneral->deleteLocation($ids);
				$this->_redirect(BASE_URL.'general/locations?msg=data_saved');
				break;
			case 'unpublish':
			case 'publish':
				$ids = $this->_request->getPost('ids');
				$this->objGeneral->publishLocation($ids, $strAction=='publish' ? 1 : 0);
				$this->_redirect(BASE_URL.'general/locations?msg=data_saved');
				break;
			default:
				$id = $this->_request->getParam('id', '-1');
				if( $id != '-1')
				{
					$this->_helper->layout->disableLayout();
					$this->view->request_ajax = true;
					$this->view->arrItem = $this->objGeneral->getLocations($id);
					return;
				}
		}
		$this->view->arrItems = $this->objGeneral->getLocations();
	}

	public function industriesAction()
	{
		$strAction = $this->_request->getPost('action');
		switch($strAction)
		{
			case 'save':
				$arrData = array(
					'industry_id' => $this->_request->getPost('id'),
					'industry_name_en' => $this->_request->getPost('name_en'),
					'industry_name_vi' => $this->_request->getPost('name_vi'),
					'industry_priority' => $this->_request->getPost('priority'),
					'industry_status'	=> $this->_request->getPost('publish')
				);
				$this->objGeneral->saveIndustry($arrData);
				$this->_redirect(BASE_URL.'general/industries?msg=data_saved');
				break;
			case 'delete':
				$ids = $this->_request->getPost('ids');
				$this->objGeneral->deleteIndustry($ids);
				$this->_redirect(BASE_URL.'general/industries?msg=data_saved');
				break;
			case 'unpublish':
			case 'publish':
				$ids = $this->_request->getPost('ids');
				$this->objGeneral->publishIndustry($ids, $strAction=='publish' ? 1 : 0);
				$this->_redirect(BASE_URL.'general/industries?msg=data_saved');
				break;
			default:
				$id = $this->_request->getParam('id', '-1');
				if( $id != '-1')
				{
					$this->_helper->layout->disableLayout();
					$this->view->request_ajax = true;
					$this->view->arrItem = $this->objGeneral->getIndustries($id);
					return;
				}
		}
		$this->view->arrItems = $this->objGeneral->getIndustries();
	}

	public function levelsAction()
	{
		$strAction = $this->_request->getPost('action');
		switch($strAction)
		{
			case 'save':
				$arrData = array(
					'level_id' => $this->_request->getPost('id'),
					'level_name_en' => $this->_request->getPost('name_en'),
					'level_name_vi' => $this->_request->getPost('name_vi'),
					'level_priority' => $this->_request->getPost('priority'),
					'level_status'	=> $this->_request->getPost('publish')
				);
				$this->objGeneral->saveLevel($arrData);
				$this->_redirect(BASE_URL.'general/levels?msg=data_saved');
				break;
			case 'delete':
				$ids = $this->_request->getPost('ids');
				$this->objGeneral->deleteLevel($ids);
				$this->_redirect(BASE_URL.'general/levels?msg=data_saved');
				break;
			case 'unpublish':
			case 'publish':
				$ids = $this->_request->getPost('ids');
				$this->objGeneral->publishLevel($ids, $strAction=='publish' ? 1 : 0);
				$this->_redirect(BASE_URL.'general/levels?msg=data_saved');
				break;
			default:
				$id = $this->_request->getParam('id', '-1');
				if( $id != '-1')
				{
					$this->_helper->layout->disableLayout();
					$this->view->request_ajax = true;
					$this->view->arrItem = $this->objGeneral->getJobLevels($id);
					return;
				}
		}
		$this->view->arrItems = $this->objGeneral->getJobLevels();
	}

	public function degreesAction()
	{
		$strAction = $this->_request->getPost('action');
		switch($strAction)
		{
			case 'save':
				$arrData = array(
					'degree_id' => $this->_request->getPost('id'),
					'degree_name_en' => $this->_request->getPost('name_en'),
					'degree_name_vi' => $this->_request->getPost('name_vi'),
					'degree_priority' => $this->_request->getPost('priority'),
					'degree_status'	=> $this->_request->getPost('publish')
				);
				$this->objGeneral->saveDegree($arrData);
				$this->_redirect(BASE_URL.'general/degrees?msg=data_saved');
				break;
			case 'delete':
				$ids = $this->_request->getPost('ids');
				$this->objGeneral->deleteDegree($ids);
				$this->_redirect(BASE_URL.'general/degrees?msg=data_saved');
				break;
			case 'unpublish':
			case 'publish':
				$ids = $this->_request->getPost('ids');
				$this->objGeneral->publishDegree($ids, $strAction=='publish' ? 1 : 0);
				$this->_redirect(BASE_URL.'general/degrees?msg=data_saved');
				break;
			default:
				$id = $this->_request->getParam('id', '-1');
				if( $id != '-1')
				{
					$this->_helper->layout->disableLayout();
					$this->view->request_ajax = true;
					$this->view->arrItem = $this->objGeneral->getJobDegrees($id);
					return;
				}
		}
		$this->view->arrItems = $this->objGeneral->getJobDegrees();
	}

	public function countriesAction()
	{
		if($this->_request->getPost('action')=='save')
		{
			$arrData = array(
				'country_id' => '',
				'country_en' => $this->_request->getPost('name_en'),
				'country_vi' => $this->_request->getPost('name_vi'),
				'country_priority' => $this->_request->getPost('priority'),
				'publish'	=> $this->_request->getPost('publish')
			);
			$this->objGeneral->saveCountry($arrData);
			die('saved');
		}
		$this->view->arrItems = $this->objGeneral->getCountries();
	}
}
